package com.clkj.zhonghui.pojo.common;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;


public class RequestHandler {
    private String tokenUrl;
    private String gateUrl;
    private String notifyUrl;
    private String appid;
    private String appkey;
    private String partnerkey;
    private String appsecret;
    private String key;
    private SortedMap parameters;
    private String Token;
    private String charset;
    private String debugInfo;
    private String last_errcode;
    private HttpServletRequest request;
    private HttpServletResponse response;

    public RequestHandler(HttpServletRequest request,
                          HttpServletResponse response) {
        this.last_errcode = "0";
        this.request = request;
        this.response = response;

        this.charset = "UTF-8";
        this.parameters = new TreeMap();

        this.notifyUrl = "https://gw.tenpay.com/gateway/simpleverifynotifyid.xml";
    }

    public void init(String app_id, String app_secret, String key, String partnerKsy) {



        this.last_errcode = "0";
        this.Token = "token_";
        this.debugInfo = "";
        this.appid = app_id;
        this.partnerkey = key;
        this.appsecret = app_secret;
        this.key = partnerKsy;
    }


    public String getLasterrCode() {
        return this.last_errcode;
    }

    public String getGateUrl() {
        return this.gateUrl;
    }

    public String getParameter(String parameter) {
        String s = (String) this.parameters.get(parameter);
        return s == null ? "" : s;
    }

    public void setKey(String key) {
        this.partnerkey = key;
    }

    public void setAppKey(String key) {
        this.appkey = key;
    }

    public String UrlEncode(String src) throws UnsupportedEncodingException {
        return URLEncoder.encode(src, this.charset).replace("+", "%20");
    }

    public String genPackage(SortedMap<String, Object> packageParams)
            throws UnsupportedEncodingException {
        String sign = createSign(packageParams);

        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            sb.append(k + "=" + UrlEncode(v) + "&");
        }
        String packageValue = new StringBuilder("sign=").append(sign)
                .toString();

        return packageValue;
    }

    public String getWXSign(JSONObject json, String appKey) {

        List<String> signList = new ArrayList<String>();
        Iterator<String> keys = (Iterator<String>) json.values();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            signList.add(key);
        }

        Collections.sort(signList);
        String sign = "";
        for (int i = 0; i < signList.size(); i++) {
            String key = signList.get(i);
            String value = json.getString(key);
            sign = sign + key + "=" + value + "&";
        }

        sign = sign + "key=" + appKey;

        sign = MD5Util.MD5Encode(sign, "UTF-8").toUpperCase();
        return sign;
    }

    public String createSign(SortedMap<String, Object> packageParams) {
        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = entry.getKey().toString();
            String v = entry.getValue().toString();
            if ((v != null) && (!"".equals(v)) && (!"sign".equals(k))
                    && (!"key".equals(k))) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + getKey());
        String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();

        return sign;
    }

    public boolean createMd5Sign(String signParams) {
        StringBuffer sb = new StringBuffer();
        Set es = this.parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if ((!"sign".equals(k)) && (v != null) && (!"".equals(v))) {
                sb.append(k + "=" + v + "&");
            }
        }
        String enc = com.clkj.zhonghui.pojo.common.TenpayUtil.getCharacterEncoding(this.request,
                this.response);
        String sign = MD5Util.MD5Encode(sb.toString(), enc).toLowerCase();

        String tenpaySign = getParameter("sign").toLowerCase();

        setDebugInfo(sb.toString() + " => sign:" + sign + " tenpaySign:"
                + tenpaySign);

        return tenpaySign.equals(sign);
    }

    public String parseXML() {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Set es = this.parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if ((v != null) && (!"".equals(v)) && (!"appkey".equals(k))) {
                sb.append("<" + k + ">" + getParameter(k) + "</" + k + ">\n");
            }
        }
        sb.append("</xml>");
        return sb.toString();
    }

    protected void setDebugInfo(String debugInfo) {
        this.debugInfo = debugInfo;
    }

    public void setPartnerkey(String partnerkey) {
        this.partnerkey = partnerkey;
    }

    public String getDebugInfo() {
        return this.debugInfo;
    }

    public String getKey() {
        return this.key;
    }

}
